// TWDatePickerView.m
// SYEdu_Company
//
// Created by 尚朋 on 2019/4/18.
// Copyright © 2019年 qhths. All rights reserved.
//
// @class  TWDatePickerView
// abstract  <#摘要#>
// description  <#描述#>


#import "TWDatePickerView.h"
#import "TWDatePicker.h"
#import "MMHeader.h"

@interface TWDatePickerView ()<TWDatePickerDelegate>
{
    NSDate * pickerDate;
}
@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong)TWDatePicker *picker;

@property (nonatomic, strong) UILabel * placeholderLabel;

@property (nonatomic, strong) UIButton * sureBtn;

@property (nonatomic, strong) UIButton * cancleBtn;

@end
@implementation TWDatePickerView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loadView];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        [self loadView];
    }
    return self;
}

- (void)loadView{
    
    //移除所有子视图
    self.backgroundColor = [UIColor blackColor];
//    [UIColor colorWithHexString:@"" alpha:0.0];

    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_WIDTH, SCREEN_HEIGHT, self.picker.frame.size.height+40)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self.bgView addSubview:self.picker];
    [self addSubview:self.bgView];

    
    self.sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    self.sureBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    self.sureBtn.frame = CGRectMake(SCREEN_WIDTH-60-15, 0, 60, 40);
    [self.sureBtn addTarget:self action:@selector(sureBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.sureBtn];
    
    self.cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    self.cancleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.cancleBtn.frame = CGRectMake(15, 0, 60, 40);
    [self.cancleBtn addTarget:self action:@selector(cancleBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.cancleBtn];
    
    self.placeholderLabel = [[UILabel alloc]init];
//    self.placeholderLabel.textColor = [UIColor colorWithHexString:@"999999"];
    self.placeholderLabel.textColor = [UIColor grayColor];
    self.placeholderLabel.font = [UIFont systemFontOfSize:14];
    self.placeholderLabel.textAlignment = NSTextAlignmentCenter;
    self.placeholderLabel.frame = CGRectMake(self.cancleBtn.frame.origin.y + self.cancleBtn.frame.size.height, 0, self.sureBtn.frame.origin.x-(self.cancleBtn.frame.origin.x + self.cancleBtn.frame.size.width), 40);
    [self.bgView addSubview:self.placeholderLabel];

    
    
    
}

- (TWDatePicker *)picker{
    if (!_picker) {
        _picker = [[TWDatePicker alloc]initWithFrame:CGRectMake(0, 40, (SCREEN_WIDTH-30), 200)];
        _picker.backgroundColor = [UIColor whiteColor];
        _picker.delegate = self;
        _picker.tag = 0;
    }
    return _picker;
}


- (void)show{
    
    UIView * v = [[[UIApplication sharedApplication].delegate window] viewWithTag:209999];
    if (v) {
        [v removeFromSuperview];
    }
    self.frame = [UIScreen mainScreen].bounds;
    self.tag = 209999;
    [[[UIApplication sharedApplication].delegate window] addSubview:self];
    

    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
        self.bgView.frame = CGRectMake(0, SCREEN_HEIGHT-self.bgView.frame.size.height, SCREEN_WIDTH, self.bgView.frame.size.height);
    } completion:^(BOOL finished) {
    }];
    
}

- (void)dismiss{
    
    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundColor = [UIColor clearColor];
//        [UIColor colorWithHexString:@"333333" alpha:0];
        self.bgView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, self.bgView.frame.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self dismiss];
}


- (void)datePicker:(TWDatePicker *)picker scrollDidEnd:(NSDate *)date{
    
    pickerDate = date;
    if (self.pickerBlock) {
        self.pickerBlock(date);
    }
}

- (void)sureBtnClicked{
    if (pickerDate) {
        if (self.pickerBlock) {
            self.pickerBlock(pickerDate);
        }
    }
    [self dismiss];
}

- (void)cancleBtnClicked{
    [self dismiss];
}


//@property(nonatomic,strong) NSDate * date;
//
//@property(nonatomic,strong) NSDate * maxDate;//最大时间
//
//@property(nonatomic,strong) NSDate * minDate;//最小时间
//
//@property(nonatomic,strong) NSDate * selectDate;

- (void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    self.placeholderLabel.text = _placeholder;
}

- (void)setDate:(NSDate *)date{
    _date = date;
    self.picker.date = _date;
}

- (void)setMaxDate:(NSDate *)maxDate{
    _maxDate = maxDate;
    self.picker.maxDate = _maxDate;
}

- (void)setMinDate:(NSDate *)minDate{
    _minDate = minDate;
    self.picker.minDate = _minDate;
}

- (void)setSelectDate:(NSDate *)selectDate{
    _selectDate = selectDate;
    self.picker.selectDate = selectDate;
}
@end
